/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Langue;
import com.applicatioN.entities.MaitriserL;
import com.applicatioN.entities.MaitriserLPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class MaitriserLJpaController implements Serializable {

    public MaitriserLJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(MaitriserL maitriserL) throws PreexistingEntityException, Exception {
        if (maitriserL.getMaitriserLPK() == null) {
            maitriserL.setMaitriserLPK(new MaitriserLPK());
        }
        maitriserL.getMaitriserLPK().setIntitule(maitriserL.getLangue().getIntitule());
        maitriserL.getMaitriserLPK().setIdentifiant(maitriserL.getCandidat().getIdentifiant());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Langue langue = maitriserL.getLangue();
            if (langue != null) {
                langue = em.getReference(langue.getClass(), langue.getIntitule());
                maitriserL.setLangue(langue);
            }
            em.persist(maitriserL);
            if (langue != null) {
                langue.getMaitriserLCollection().add(maitriserL);
                langue = em.merge(langue);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findMaitriserL(maitriserL.getMaitriserLPK()) != null) {
                throw new PreexistingEntityException("MaitriserL " + maitriserL + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(MaitriserL maitriserL) throws NonexistentEntityException, Exception {
        maitriserL.getMaitriserLPK().setIntitule(maitriserL.getLangue().getIntitule());
        maitriserL.getMaitriserLPK().setIdentifiant(maitriserL.getCandidat().getIdentifiant());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaitriserL persistentMaitriserL = em.find(MaitriserL.class, maitriserL.getMaitriserLPK());
            Langue langueOld = persistentMaitriserL.getLangue();
            Langue langueNew = maitriserL.getLangue();
            if (langueNew != null) {
                langueNew = em.getReference(langueNew.getClass(), langueNew.getIntitule());
                maitriserL.setLangue(langueNew);
            }
            maitriserL = em.merge(maitriserL);
            if (langueOld != null && !langueOld.equals(langueNew)) {
                langueOld.getMaitriserLCollection().remove(maitriserL);
                langueOld = em.merge(langueOld);
            }
            if (langueNew != null && !langueNew.equals(langueOld)) {
                langueNew.getMaitriserLCollection().add(maitriserL);
                langueNew = em.merge(langueNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                MaitriserLPK id = maitriserL.getMaitriserLPK();
                if (findMaitriserL(id) == null) {
                    throw new NonexistentEntityException("The maitriserL with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(MaitriserLPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaitriserL maitriserL;
            try {
                maitriserL = em.getReference(MaitriserL.class, id);
                maitriserL.getMaitriserLPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The maitriserL with id " + id + " no longer exists.", enfe);
            }
            Langue langue = maitriserL.getLangue();
            if (langue != null) {
                langue.getMaitriserLCollection().remove(maitriserL);
                langue = em.merge(langue);
            }
            em.remove(maitriserL);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<MaitriserL> findMaitriserLEntities() {
        return findMaitriserLEntities(true, -1, -1);
    }

    public List<MaitriserL> findMaitriserLEntities(int maxResults, int firstResult) {
        return findMaitriserLEntities(false, maxResults, firstResult);
    }

    private List<MaitriserL> findMaitriserLEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MaitriserL.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public MaitriserL findMaitriserL(MaitriserLPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(MaitriserL.class, id);
        } finally {
            em.close();
        }
    }

    public int getMaitriserLCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<MaitriserL> rt = cq.from(MaitriserL.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
